"""
配置管理模块
"""
from pydantic_settings import BaseSettings, SettingsConfigDict
from typing import Dict, Any


class Settings(BaseSettings):
    """应用配置"""
    
    # 微信公众号配置
    wechat_app_id: str
    wechat_app_secret: str
    wechat_base_url: str = "http://121.40.250.185"
    
    # DeepSeek API配置
    deepseek_api_key: str
    deepseek_base_url: str = "https://api.deepseek.com"
    
    # 通义万相配置
    tongyi_api_key: str
    tongyi_base_url: str = "https://dashscope.aliyuncs.com"
    
    # 数据库配置
    db_host: str
    db_name: str
    db_user: str
    db_password: str
    db_port: int = 3306
    
    # 应用配置
    app_port: int = 8000
    environment: str = "production"
    
    # 调度配置
    cron_schedule: str = "0 9 * * *"
    max_concurrent_generations: int = 3
    
    @property
    def database_url(self) -> str:
        """数据库连接字符串"""
        return f"mysql+aiomysql://{self.db_user}:{self.db_password}@{self.db_host}:{self.db_port}/{self.db_name}?charset=utf8mb4"
    
    @property
    def sync_database_url(self) -> str:
        """同步数据库连接字符串"""
        return f"mysql+pymysql://{self.db_user}:{self.db_password}@{self.db_host}:{self.db_port}/{self.db_name}?charset=utf8mb4"
    
    model_config = SettingsConfigDict(env_file=".env", case_sensitive=False)


class AppConfig:
    """应用业务配置"""
    
    generation: Dict[str, Any] = {
        "title_max_length": 60,
        "summary_max_length": 100,
        "content_min_length": 1500,
        "image_quality": "high",
        "concurrent_limit": 3
    }
    
    wechat: Dict[str, Any] = {
        "token_refresh_interval": 110,  # 分钟
        "upload_retry_times": 3,
        "draft_auto_save": True
    }
    
    scheduling: Dict[str, Any] = {
        "enabled": True,
        "default_cron": "0 9 * * *",
        "max_daily_articles": 5
    }
    
    deepseek: Dict[str, Any] = {
        "model": "deepseek-reasoner",
        "max_tokens": 4000,
        "temperature": 0.7,
        "top_p": 0.9,
        # 思考模式专用设置
        "title_max_tokens": 1000,  # 标题生成需要更多 token 用于思考（reasoner模型需要更多）
        "summary_max_tokens": 2000,  # 摘要生成需要更多 token（思考模式需要大量token用于推理）
        "content_max_tokens": 4000   # 正文生成
    }
    
    tongyi: Dict[str, Any] = {
        "model": "wanx-v1",
        "size": "1024*1024",
        "quality": "high",
        "style": "realistic"
    }


# 全局设置实例
settings = Settings()
app_config = AppConfig()